package com.android.djlmapp.activity.my;

import android.content.Intent;
import android.os.Build;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.home.web.base.BaseWebActivity;
import com.android.djlmapp.http.ComPareUtils;
import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.token.GetUrlStr;

import butterknife.BindView;

public class ProblemActivity extends BaseWebActivity {
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;


    @Override
    protected void initView() {
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.GONE);
        tvName.setText("常见问题");
        String signUrl = new ComPareUtils.ComPareBuilder()
                .put("token", getToken())
                .put("version", getVersion())
                .create().getSignString();

        String url = GlobalUrls.FIND_CJWT + "?token=" + getToken() + "&version=" + getVersion() + "&sign=" + signUrl;
        setWebViewClient(mWebViewClient);
        setWebUrl(url);
        super.initView();
    }

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String str) {
            if (str.contains("login_out")) {
                GetUrlStr.getStrUrl(ProblemActivity.this);
                return true;
            } else if (str.contains("https://api.djlm.wang")) {
                // 美恰(在线客服)
                goToMeiQia();
                return true;
            } else if (str.contains("https://www.djlm.wang")) {
                // 意见反馈
                startActivity(new Intent(ProblemActivity.this, FeedBackActivity.class));
                return true;
            }
            return super.shouldOverrideUrlLoading(view, str);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                String str = request.getUrl().getHost();
                if (str.contains("login_out")) {
                    GetUrlStr.getStrUrl(ProblemActivity.this);
                    return true;
                } else if (str.contains("api.djlm.wang")) {
                    // 美恰(在线客服)
                    goToMeiQia();
                    return true;
                } else if (str.contains("www.djlm.wang")) {
                    // 意见反馈
                    startActivity(new Intent(ProblemActivity.this, FeedBackActivity.class));
                    return true;
                }
            }
            return false;
        }

    };
}
